/**
 * @format
 * @Author: TC
 * @Date: 2021-09-27 14:53:51
 * @Last Modified by: TC
 * @Last Modified time: 2021-09-29 11:16:02
 */

import { _decorator, Component, director, Node, UITransform } from "cc";
import { app } from "../../App";
import { LoadingWindow } from "../../CCComponent/Window/LoadingWindow";
import { IBaseScene } from "../../Common/BaseInterface";
import { EUILayer, UI_LAYER } from "../../Const/ComponentConst";
import { EFrameEventConst } from "../../Const/FrameEventConst";
import { BaseComponent } from "./BaseComponent";
const { ccclass, property } = _decorator;

/**
 * 基础场景类
 */
@ccclass("BaseScene")
export class BaseScene extends BaseComponent implements IBaseScene {
    protected init(): void {
        super.init();
        this.initLayers();
    }

    protected registerEvent(): void {
        super.registerEvent();
        this.register(EFrameEventConst.UPDATE_SCENE, this.onRefresh);
    }

    protected unregisterEvent(): void {
        super.unregisterEvent();
        this.unregister(EFrameEventConst.UPDATE_SCENE);
    }

    onEnable(): void {
        this.onShow();
    }
    onDestroy() {
        super.onDestroy();
        // tApp.poolMgr.releaseAll();
    }

    /**
     * 初始化层级
     */
    initLayers(): void {
        const canvasNode = director.getScene().getChildByName("Canvas");
        UI_LAYER.forEach(layerName => {
            const nodeLayer = new Node(layerName);
            const utf = nodeLayer.addComponent(UITransform);
            utf.setContentSize(canvasNode.getComponent(UITransform).contentSize);
            canvasNode.addChild(nodeLayer);
        });
    }

    /**
     * 获取层级节点
     * @param layerName
     * @returns
     */
    getLayer(layerName: EUILayer): Node {
        const canvasNode = director.getScene().getChildByName("Canvas");
        return canvasNode.getChildByName(layerName);
    }
}
